package main

import (
	"Test/xunjian_v3/tools"
	"encoding/json"
	"fmt"
	"github.com/modood/table"
	"net/http"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(200)
	w.Write([]byte("巡检程序状态正常"))
}

func apiStatusHandler(w http.ResponseWriter, r *http.Request) {
	tools.PrintRequestLog(r)

	tmpDataSlice := make([]apiRowData, 0, 100)
	for _, r := range apiRowsSli {
		if r.Id != "" {
			//fmt.Println(r.Id)
			//fmt.Printf("%+v\n", r)
			tmpDataSlice = append(tmpDataSlice, r)
		}
	}
	//fmt.Println(tmpDataSlice)

	respSt := struct {
		Data []apiRowData `json:"data"`
		Code int          `json:"code"`
	}{
		Data: tmpDataSlice,
		Code: 200,
	}

	w.Header().Set("content-type", "text/json")
	output, _ := json.Marshal(&respSt)
	fmt.Println(string(output))
	fmt.Fprintf(w, "%s", string(output))
}

func tableHandler(w http.ResponseWriter, r *http.Request) {
	tools.PrintRequestLog(r)

	tmpDataSlice := make([]apiRowData, 0, 100)
	for _, r := range apiRowsSli {
		if r.Id != "" {
			//fmt.Println(r.Id)
			//fmt.Printf("%+v\n", r)
			tmpDataSlice = append(tmpDataSlice, r)
		}
	}
	t := table.Table(tmpDataSlice)
	fmt.Fprintf(w, "%s", t)
}

func timeHandler(w http.ResponseWriter, r *http.Request) {
	tools.PrintRequestLog(r)

	resp := struct {
		CheckTime string `json:"check_time"`
		Code      int    `json:"code"`
	}{
		CheckTime: checkTime.Format("2006-01-02 15:04:05"),
		Code:      200,
	}

	w.Header().Set("content-type", "text/json")
	output, _ := json.Marshal(&resp)
	fmt.Println(string(output))
	fmt.Fprintf(w, "%s", string(output))
	//log.Printf("%s", s)

}
